﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace FYPHP
{
    public partial class SignIn : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
          
            ////Clear the cookies
            //HttpCookie myCookie = new HttpCookie("MyTestCookie");
            //// Set the cookie expiration date.
            //myCookie.Expires = DateTime.Now.AddDays(-1);

            HttpCookie myCookie = new HttpCookie("MyTestCookie");
            myCookie = Request.Cookies["MyTestCookie"];

            if (myCookie != null)
            {
                try
                {
                    SqlCommand cmdSearch;
                    SqlDataReader dtrSearch;
                    conn.Open();

                    cmdSearch = new SqlCommand("Select * From Users Where u_id=@UID", conn);
                    cmdSearch.Parameters.AddWithValue("@UID", Convert.ToInt32(myCookie.Value));
                    dtrSearch = cmdSearch.ExecuteReader();

                    if (dtrSearch.HasRows)
                    {
                        if (dtrSearch.Read())
                        {
                            Session["Username"] = dtrSearch["username"].ToString();
                            Session["ID"] = dtrSearch["u_id"].ToString();

                            if (dtrSearch["u_position"].ToString() == "Member")
                                Session["Role"] = "Member";
                            else if (dtrSearch["u_position"].ToString() == "Admin")
                                Session["Role"] = "Admin";
                            else if (dtrSearch["u_position"].ToString() == "ShopOwner")
                                Session["Role"] = "ShopOwner";

                            Response.Redirect("ViewStaffProfile.aspx");
                        }
                    }
                    dtrSearch.Close();
                    conn.Close();
                }
                catch (SqlException a)
                {
                }
            }
        }

        protected void BtnSignin_Click(object sender, EventArgs e)
        {
            String Position=null;

            try
            {
                SqlCommand cmdSearch;
                SqlDataReader dtrSearch;
                conn.Open();

                cmdSearch = new SqlCommand("Select * From Users Where Username=@name And Password=@pw", conn);
                cmdSearch.Parameters.AddWithValue("@name", txtUsername.Text);
                cmdSearch.Parameters.AddWithValue("@pw", txtPassword.Text);
                dtrSearch = cmdSearch.ExecuteReader();

                if (dtrSearch.HasRows)
                {
                    if (dtrSearch.Read())
                    {
                        Session["Username"] = txtUsername.Text;
                        Session["ID"] = dtrSearch["u_id"].ToString();

                        Position = dtrSearch["u_position"].ToString();

                        if (Position == "Member")
                            Session["Role"] = "Member";
                        else if (Position == "Admin")
                            Session["Role"] = "Admin";
                        else if (Position == "ShopOwner")
                            Session["Role"] = "ShopOwner";

                        if (chkSign.Checked == true)
                        {
                            HttpCookie myCookie = new HttpCookie("MyTestCookie");
                            // Set the cookie value.
                            myCookie.Value = Session["ID"].ToString();
                            // Set the cookie expiration date.
                            myCookie.Expires = DateTime.Now.AddDays(7);
                            // Add the cookie.
                            Response.Cookies.Add(myCookie);

                            Response.Write("<p> The cookie has been written.");
                        }
                    }
                }
                else
                    MessageBox("Invalid User, Please Try Again!!!");

                if (Position == "Member" || Position == "ShopOwner")
                    Response.Redirect("ViewMemberProfile.aspx");
                else if (Position == "Admin")
                    Response.Redirect("ViewStaffProfile.aspx");

                dtrSearch.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
            }  

        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }
    }
}